package com.aj.easylibrary.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;

/**
 * Created by gaj on 2019/3/25.
 */
public class FileUtils {

    public static String saveBitMapToCache(Context context, String fileName, Bitmap bitmap) {
        File path = context.getFilesDir();
        return saveBitmap(path, fileName, bitmap);
    }

    public static String saveBitMapToSDCar(Context context, String fileName, Bitmap bitmap) {
        File file = getExternalStorageDirectoryPath();
        if (file != null) {
            return saveBitmap(file, fileName, bitmap);
        } else {
            return "";
        }
    }

    public static File getExternalStorageDirectoryPath() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File file = new File(Environment.getExternalStorageDirectory() + "/img/");
            file.mkdir();
            return file;
        } else {
            return null;
        }
    }

    public static String saveBitmap(File dir, String fileName, Bitmap bitmap) {
        File file = new File(dir, fileName);
        if (file.exists()) {
            file.delete();
        }
        System.out.println(file.getPath());
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            return file.getPath();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }

//    public static boolean copyFile(String oldPath, String newPath) {
//        InputStream inStream = null;
//        FileOutputStream fs = null;
//        try {
//            int bytesum = 0;
//            int byteread = 0;
//            File oldfile = new File(oldPath);
//            if (oldfile.exists()) { //文件存在时
//                inStream = new FileInputStream(oldPath); //读入原文件
//                fs = new FileOutputStream(newPath);
//                byte[] buffer = new byte[1024];
//                int length;
//                while ((byteread = inStream.read(buffer)) != -1) {
//                    bytesum += byteread; //字节数 文件大小
//                    fs.write(buffer, 0, byteread);
//                }
//                return true;
//            }
//        } catch (Exception e) {
//            System.out.println("复制单个文件操作出错");
//            e.printStackTrace();
//        } finally {
//            if (fs != null) {
//                try {
//                    fs.flush();
//                    fs.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//
//            }
//            try {
//                if (inStream != null) {
//                    inStream.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//
//        }
//        return false;
//
//    }

    public static boolean  copyFile(File src, File dest) {
        boolean result = false;
        if (dest!= null && dest.exists()) {
            dest.delete(); // delete file
        }
        try {
            dest.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        FileChannel srcChannel = null;
        FileChannel dstChannel = null;

        try {
            srcChannel = new FileInputStream(src).getChannel();
            dstChannel = new FileOutputStream(dest).getChannel();
            srcChannel.transferTo(0, srcChannel.size(), dstChannel);
            result = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            return result;
        }
        try {
            srcChannel.close();
            dstChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


    public static File getSaveFile(Context context) {
        File file = new File(context.getFilesDir(), "pic.jpg");
        return file;
    }

    public static File getSaveFile(Context context, String fileName) {
        File file = new File(context.getFilesDir(), fileName);
        return file;
    }

}
